/**
 * Created by 97203 on 2018/06/08.
 */
$(function () {
    //1，将所用的变量都抽取出来，放在页面加载后
    var cpt_datagrid = $("#cpt_datagrid");
    var cpt_dialog = $("#cpt_dialog");
    var cpt_form = $("#cpt_form");
    var import_dialog = $("#import_dialog");
    cpt_datagrid.datagrid({
        url: '/compensation/query.do',
        fitColumns: true,
        fit: true,
        pagination: true,    //分页条
        rownumbers: true,     //行号列
        singleSelect: true,
        sortName: 'hireDate',
        sortOrder: 'desc',
        toolbar: '#tb',
        onClickRow: function (index, row) {
            if (row.status) {
                $("#changeState_btn").linkbutton({
                    text: '离职'
                })
            } else {
                $("#changeState_btn").linkbutton({
                    text: '复职'
                })
            }
        },
        columns: [[
            {field: 'date', title: '日期', width: 100},
            {field: 'employee1', title: '姓名', width: 100,formatter: function (value,row) {
                return row.employee.username ? row.employee.username : "";
            }},
            {
                field: 'dept', title: '部门', width: 100, formatter: function (value) {
                return value ? value.name : "";
            }
            },
            {field: 'employee2', title: '联系电话', width: 100,formatter: function (value,row) {
                return row.employee.tel ? row.employee.tel : "";
            }},
            {field: 'employee3', title: '邮箱', width: 100,formatter: function (value,row) {
                return row.employee.email ? row.employee.email : "";
            }},
            {field: 'basicMoney', title: '基本工资', width: 100},
            {field: 'bonus', title: '奖金', width: 100},
            {field: 'sumMoney', title: '总薪资', width: 100},
            {
                field: 'status', title: '状态', width: 100, formatter: function (value) {
                return value ? "<font color='green'>在职</font>" : "<font color='red'>离职</font>";
            }
            },
            {field:'queryTask',title:'查看薪资明细',width:$(this).width()*0.1,align:'center',
                formatter:function (value,row,index) {
                    var str = '<a href="#" name="opera" class="easyui-linkbutton"  onclick="getChild()"></a>';
                    return str;
                }},
        ]],
        onLoadSuccess:function (data) {
            $("a[name='opera']").linkbutton({
                text:'查看',
                toggle:true,
                // iconCls:'icon-redo',
                plain:true
            })

        },
        // onClickRow:function(){    //单击进行操作的方法
        //         var row = cpt_datagrid.datagrid('getSelected');//获取选中行的数据
        //         console.log("7777");
        //         if (!row){
        //             return;//为防止意外情况可以选择加上此判断
        //         }
    })


    cpt_dialog.dialog({
        width: 320,
        height: 400,
        buttons: '#btn',
        closed: true,
        //在窗口关闭的时候触发事件
        onClose: function () {
            //清空表单数据
            cpt_form.form('clear');
        }
    })
    import_dialog.dialog({
        width: 350,
        height: 100,
        closed: true
    })

    //2，将所有方法同一交给一个对象管理
    var methodObj = {
        add: function () {
            //设置标题
            cpt_dialog.dialog('setTitle', '新建任务单');
            //打开弹出框
            $("#password_tr").show();
            cpt_dialog.dialog('open');
        },
        edit: function () {
            var row = cpt_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要编辑的数据！', 'warning');
                return;
            }
            if (row.dept) {
                row["dept.name"] = row.dept.name;
            }

            if (row.employee) {
                row["employee.username"] = row.employee.username;
                row["employee.tel"] = row.employee.tel;
                row["employee.email"] = row.employee.email;
            }

            $.get('/compensation/getRoleIdByEmpId.do', {compensationId: row.id}, function (data) {
                $("#role_combobox").combobox('setValues', data)
            })




            //把数据回显到表单中
            cpt_form.form('load', row);
            //设置标题
            cpt_dialog.dialog('setTitle', '编辑薪资');
            //打开弹出框
            cpt_dialog.dialog('open');

        },
        changeState: function () {
            var row = cpt_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要操作的数据！', 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您想要执行操作吗？', function (r) {
                if (r) {
                    $.get('/compensation/changeState.do', {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功！', 'info', function () {
                                cpt_datagrid.datagrid('reload');
                            });
                        } else {
                            $.messager.alert('温馨提示', 'data.msg', 'warning');
                        }
                    })
                }
            });
        },
        changePwd: function () {
            var row = cpt_datagrid.datagrid('getSelected');
            if (!row) {
                $.messager.alert('温馨提示', '请选中要操作的数据！', 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您想要执行操作吗？', function (r) {
                if (r) {
                    $.get('/compensation/changePassword.do', {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功！', 'info', function () {
                                cpt_datagrid.datagrid('reload');
                            });
                        } else {
                            $.messager.alert('温馨提示', 'data.msg', 'warning');
                        }
                    })
                }
            });
        },
        reload: function () {
            cpt_datagrid.datagrid('reload');
        },
        save: function () {
            //提交ajax
            cpt_form.form('submit', {
                url: '/compensation/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data)
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功！', 'info', function () {
                            //关闭弹出框
                            methodObj.cancel();
                            //在dataGrid重新加载数据
                            cpt_datagrid.datagrid('reload');
                        });
                    } else {
                        $.messager.alert('温馨提示', 'data.msg', 'warning');
                    }
                }
            })
        },
        cancel: function () {
            cpt_dialog.dialog('close');
        },
        query: function () {
            var keyword = $("[name=keyword]").val();
            var deptId = $("[name=deptId]").val();
            var beginTime = $("[name=beginTime]").val();
            var endTime = $("[name=endTime]").val();
            cpt_datagrid.datagrid('load', {
                keyword: keyword,
                deptId: deptId,
                beginTime: beginTime,
                endTime: endTime,
            })
        },
        exportXls: function () {
            var keyword = $("[name=keyword]").val();
            var deptId = $("[name=deptId]").val();
            var beginTime = $("[name=beginTime]").val();
            var endTime = $("[name=endTime]").val();
            window.location.href = "/compensation/exportXls.do?keyword=" + keyword + "&deptId=" + deptId+"&beginTime="+beginTime+"&endTime="+endTime;
        },
        importXls: function () {
            import_dialog.dialog('open');
        },
    }
    //3，给按钮统一绑定点击事件
    $("a[data-cmd]").click(function () {
        //获取按钮上的data-cmd的值
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })
})
function fileSubmit() {
    var fileSize = $("#file").val();
    if(fileSize!=null && fileSize != ''){
        $("#import_form").submit();
    }else {
        $.messager.alert('温馨提示', '请选中要上传的文件！', 'warning');
    }
}

function getChild() {
    var cpt_datagrid = $("#cpt_datagrid");
    var cpt_dialog = $("#cpt_dialog");
    var cpt_form = $("#cpt_form");
    var import_dialog = $("#import_dialog");
    var row = cpt_datagrid.datagrid('getSelected');
    if (row.dept) {
        row["dept.id"] = row.dept.id;
    }

    if (row.employee) {
        row["employee.id"] = row.employee.id;
    }

    $.get('/compensation/getRoleIdByEmpId.do', {cptId: row.id}, function (data) {
        $("#role_combobox").combobox('setValues', data)
    })
    //把数据回显到表单中
    cpt_form.form('load', row);
    //设置标题
    cpt_dialog.dialog('setTitle', '编辑员工');
    //打开弹出框
    cpt_dialog.dialog('open');

}
$(function () {

    var basicMoney = Number($("input",$("#basicMoney").next("span")).val());
    var bonus = Number($("input",$("#bonus").next("span")).val());
    console.log(bonus);


    $("input",$("#basicMoney").next("span")).blur(function(){
        bonus = Number($("input",$("#bonus").next("span")).val());
        basicMoney = Number($(this).val());
        $("input",$("#sumMoney").next("span")).val(Number(basicMoney)+Number(bonus));
    })
    $("input",$("#bonus").next("span")).blur(function(){
        basicMoney = Number($("input",$("#basicMoney").next("span")).val());
        bonus = Number($(this).val());
        $("input",$("#sumMoney").next("span")).val(Number(basicMoney)+Number(bonus));
    })

})